/*
 * 文件名： BaseListFragment.java
 * 描    述：Fragment 商品列表基类
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.base;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.miguo.activity.callback.IGoods;
import com.miguo.app.AppContext;
import com.miguo.framework.adapter.QuickAdapter;
import com.miguo.framework.request.FailData;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.GridViewWithHeaderAndFooter;
import com.miguo.framework.view.pulltorefresh.PullToRefreshBase;
import com.miguo.framework.view.pulltorefresh.PullToRefreshBase.OnRefreshListenerTwo;
import com.miguo.framework.view.pulltorefresh.PullToRefreshGridView;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;
import com.miguo.utils.CommonUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class BaseListFragment<Bean, Adapter extends QuickAdapter> extends BaseFragment implements
        OnClickListener, OnRefreshListenerTwo<GridViewWithHeaderAndFooter> {

    protected View view;

    //设置网格中的列数
    protected int mNumColumns = 2;

    protected int page = 1;// 当前页
    protected int pageSize = 1;// 总页数

    protected boolean isRefresh = true;

    protected AjaxCallBack AjaxCallBack;

    protected GridViewWithHeaderAndFooter gridView;
    protected ImageView mToTopBtn;

    private List<Bean> mArrayList;
    private Adapter mAdapter;

    protected PullToRefreshGridView pullToRefresh;
    protected ImageLoader imageLoader = ImageLoader.getInstance();


    private IGoods<Bean, Adapter> mGoodsCallBack;

    protected Animation mTopUpAnima;

    protected Animation mTopDownAnima;

    protected boolean pauseOnScroll = false;

    protected boolean pauseOnFling = true;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void setGoodsCallBack(IGoods<Bean, Adapter> callBack) {
        mGoodsCallBack = callBack;
    }

    protected void findViewById() {
        pullToRefresh = (PullToRefreshGridView) view
                .findViewById(R.id.pull_refresh_grid);
        gridView = pullToRefresh.getRefreshableView();

        mToTopBtn = (ImageView) view.findViewById(R.id.toTopBtn);
        mToTopBtn.setOnClickListener(this);
        init();
    }

    protected void initView() {
        mArrayList = new ArrayList<>();
        mAdapter = mGoodsCallBack.onGoodsLoadAdapter();
        gridView.setAdapter(mAdapter);
        gridView.setOnScrollListener(mGoodsCallBack.onGoodsScroll());


        // 列数
        gridView.setNumColumns(mNumColumns);
        // 滑动到顶部和底部时出现的阴影消除 2.3以前用 android:fadingEdge="none"
        if (Build.VERSION.SDK_INT >= 9) {
            gridView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        }

        // 去除上拉下拉的箭头
        pullToRefresh.setShowIndicator(false);
        // 设置监听器
        pullToRefresh.setOnRefreshListener(this);

        mGoodsCallBack.onGoodsLoadData();
    }

    @SuppressWarnings("unchecked")
    private void init() {
        mTopUpAnima = new TranslateAnimation(0.0F, 0.0F, CommonUtils.dip2px(
                AppContext.getContext(), 48.0F), 0.0F);
        mTopUpAnima.setDuration(500L);
        mTopDownAnima = new TranslateAnimation(0.0F, 0.0F, 0.0F, CommonUtils.dip2px(
                AppContext.getContext(), 48.0F));
        mTopDownAnima.setDuration(500L);

        AjaxCallBack = new AjaxCallBack(mActivity) {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);
                pullToRefresh.onRefreshComplete();
                if (json.optInt("status") == 200) {
                    JsonData json_data = json.optJson("data");
                    JsonData json_page = json_data.optJson("page");
                    pageSize = json_page.optInt("page_size");
                    mArrayList = mGoodsCallBack.onGoodsDealList(json_data);
                    if (mArrayList.size() > 0) {
                        mAdapter.addAll(mArrayList, isRefresh);
                    } else {
                        page--;
                        Toast.show(mContext, getResources().getString(R.string.pull_to_refresh_nodata));
                    }
                    mGoodsCallBack.onGoodsFinish();
                } else {
                    page--;
                    Toast.show(mContext, json.optString("info"));
                    mGoodsCallBack.onGoodsError();
                }
            }

            @Override
            public void onRequestFail(FailData data) {
                super.onRequestFail(data);
                pullToRefresh.onRefreshComplete();
                mGoodsCallBack.onGoodsFail();
            }
        };
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<GridViewWithHeaderAndFooter> refreshView) {
        page = 1;
        isRefresh = true;
        mGoodsCallBack.onGoodsLoadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<GridViewWithHeaderAndFooter> refreshView) {
        if (pageSize >= page + 1) {
            page++;
            isRefresh = false;
            mGoodsCallBack.onGoodsLoadData();
        } else {
            Toast.show(mContext, "已经到底了!");
            pullToRefresh.onRefreshComplete();
        }
    }

    protected void refresh() {
        isRefresh = false;
        mGoodsCallBack.onGoodsRefresh();
        mGoodsCallBack.onGoodsLoadData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toTopBtn:
                gridView.setSelection(0);
                if (mToTopBtn.getVisibility() == View.VISIBLE) {
                    mToTopBtn.setVisibility(View.GONE);
                    mToTopBtn.startAnimation(mTopDownAnima);
                }
                break;
        }
    }

}
